<?php
namespace Home\Controller;

class InvestController extends \Home\Controller\BaseController
{
	/**
	 * 查询用户已签合同
	 * @author 尹雷
	 */
	public function userContract()
	{
		$param = I('params');
		if(!$param) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		$field =  'id,fund_id,contract_id,contract_name,status,create_time';
		$where['user_id'] = $this->user['userId'];
		$where['status'] = array('gt', 0);
		$option['group'] = 'contract_id';
		$contracts = D('Invest')->searchInvest($where, $option, 'create_time desc', $field);
		$this->ajaxReturn($this->result($contracts));
	}
	
	/**
	 * 查询投资记录
	 */
	public function investListBySelf()
	{
		$param = I('params');
		if(!$param) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		
		// 构造查询条件		
		$where['user_id'] = $this->user['userId'];
		if(I('data.fund_id', '', '/^\d+$/', $param)) $where['fund_id'] = $param['fund_id'];
		$where['status'] = array('gt', 0);
		
		$list = D('Invest')->searchInvest($where);
		$this->ajaxReturn($this->result($list)); 
	}
	
	/**
	 * 查询投资记录
	 */
	public function investList()
	{
		$param = I('params');
		if(!$param) $this->ajaxReturn(array('error' => 1, 'info' => '参数错误'));
		$type = I('data.type', 0, '/^[01]$/', $param);
		
		// 返回字段
		$field = 'id,user_id,mobile,fund_id,amount,create_time';
		
		// 构造查询条件
		$where = array();
		if(I('data.fund_id', '', '/^\d+$/', $param)) $where['fund_id'] = $param['fund_id'];
		$where['status'] = 1;
		
		$option['page'] = I('data.page', '', '/^\d+$/', $param) ? $param['page'] : 1;
		$option['size'] = I('data.size', '', '/^\d+$/', $param) ? $param['size'] : 5;
		
		$list = D('Invest')->searchInvest($where, $option, 'create_time desc', $field);
		$this->ajaxReturn($this->result($list));
	}
	
	/**
	 * 下单
	 * @author 尹雷
	 */
	public function addOrder()
	{
		$param = I('params');
		$data['user_id'] = $this->user['userId'];
		$data['fund_id'] = $param['fund_id'];
		$data['contract_id'] = $param['contract_id'];
		$data['amount'] = $param['amount'];
		$data['netvalue_id'] = $param['netvalue_id'];
		$data['realname'] = $param['realname'];
		$data['social_no'] = $param['social_no'];
		$data['ip'] = $param['ip'];
		$res = D('Invest')->addOrder($data, $this->user['userId']);

		$this->ajaxReturn($res);
	}
	
	/**
	 * 赎回
	 * @author 尹雷
	 */
	public function redeem()
	{
		$param = I('params');
		$data['invest_id'] = I('data.invest_id', '', '/^[1-9]\d*$/', $param);
		$data['count'] = I('data.count', '', '/^\d+$/', $param);
		$data['user_id'] = I('data.user_id', '', '/^\d+$/', $param);
		$res = D('Redeem')->addRedeem($data);
		$this->ajaxReturn($res);
	}
	
	/**
	 * 赎回时查询是否违约及违约金
	 */
	public function isDefaults()
	{
		$param = I('params');
		$investId = I('data.invest_id', '', '/^[1-9]\d*$/', $param);
		$count = I('data.count', '', '/^\d+$/', $param);
		$res = D('Invest')->defaultsInfo($count, $investId);
		$this->ajaxReturn($res);
	}
	
	/**
	 * 投资收益
	 * @author 尹雷
	 */
	public function profit()
	{
		$param = I('param');
	}
}